const yargs = require('yargs');
const create = require('../bin/create');
const build = require('../bin/build');
const app = require('../bin/app');
const login = require('../bin/login');
const push = require('../bin/push');
const pull = require('../bin/pull');
const sinon = require('sinon');

describe('生成所有系统表单的选单、参照模型', function () {
  this.timeout('10min');

  // json.reduce((ret,it)=>ret.concat(it.boList.map(b=>({...b, categoryName: it.categoryName}))) ,[])
  // await exec('login -e dev');
  const boList = [
    { displayText: '期初进货单', name: 'GoodsReceiptBeginning', categoryName: '采购管理' },
    { displayText: '采购申请单', name: 'PurchaseApply', categoryName: '采购管理' },
    { displayText: '采购订单', name: 'PurchaseOrder', categoryName: '采购管理' },
    { displayText: '进货单', name: 'GoodsReceipt', categoryName: '采购管理' },
    { displayText: '收入合同', name: 'Contract', categoryName: '项目合同' },
    { displayText: '收入确认单', name: 'RevenueConfirm', categoryName: '项目合同' },
    { displayText: '支出合同', name: 'ExpenditureContract', categoryName: '项目合同' },
    { displayText: '支出确认单', name: 'ExpenditureConfirm', categoryName: '项目合同' },
    { displayText: '项目', name: 'Project', categoryName: '项目合同' },
    { displayText: '销售订单', name: 'SalesOrder', categoryName: '销售管理' },
    { displayText: '销货单', name: 'GoodsIssue', categoryName: '销售管理' },
    { displayText: '产成品入库单', name: 'FinishedGoodsStock', categoryName: '库存核算' },
    { displayText: '其他入库单', name: 'StockIn', categoryName: '库存核算' },
    { displayText: '材料出库单', name: 'MaterialStock', categoryName: '库存核算' },
    { displayText: '其他出库单', name: 'StockOut', categoryName: '库存核算' },
    { displayText: '要货单', name: 'RequireGoods', categoryName: '库存核算' },
    { displayText: '调拨单', name: 'StockTransfer', categoryName: '库存核算' },
    { displayText: '组装拆卸单', name: 'Assembly', categoryName: '库存核算' },
    { displayText: '成本调整单', name: 'InventoryCostAdj', categoryName: '库存核算' },
    { displayText: '分量盘点单', name: 'StockCountSub', categoryName: '库存核算' },
    { displayText: '收款单', name: 'PaymentAR', categoryName: '资金往来' },
    { displayText: '付款单', name: 'PaymentAP', categoryName: '资金往来' },
    { displayText: '费用支出单', name: 'Expense', categoryName: '资金往来' },
    { displayText: '其他收入单', name: 'Revenue', categoryName: '资金往来' },
    { displayText: '银行存取款单', name: 'FinTransfer', categoryName: '资金往来' },
    { displayText: '应收应付调整单', name: 'ArapAdjust', categoryName: '资金往来' },
    { displayText: '物料清单', name: 'Bom', categoryName: '基本档案' },
    { displayText: '零售单', name: 'Retail', categoryName: '零售管理' },
    { displayText: '零售结算单', name: 'RetailSettlement', categoryName: '零售管理' },
    { displayText: '零售出库单', name: 'RetailStock', categoryName: '零售管理' }
  ];

  function exec(cmd) {
    return new Promise((resolve) => {
      console.log(cmd);
      const spy = sinon.spy(console, 'log');
      const cmc = new yargs();
      create(cmc);
      build(cmc);
      app(cmc);
      login(cmc);
      push(cmc);
      pull(cmc);
      cmc.onFinishCommand(() => {
        spy.restore();
        resolve(spy.args[0]?.join('\n'));
      });
      cmc.parse(cmd);
    });
  }

  it('生成所有单据', async function () {
    await exec(`ldap --username caizx --password '5sT4?8H18F)6^726'`);
    for (const { name, displayText, categoryName } of boList) {
      // await exec(`save -c ${categoryName} -d ${displayText} -n ${name}`);
      await exec(
        `create --billType voucherselect --billNo ${
          name.substring(0, 1).toLowerCase() + name.substring(1)
        }Select --name 选择${displayText} --module ${name} --bo ${name} -s web`
      );
      await exec('push');
      await exec(
        `create --billType refer --billNo ${
          name.substring(0, 1).toLowerCase() + name.substring(1)
        }Refer --name ${displayText} --module ${name} --bo ${name} -s web`
      );
      await exec('push');
    }
  });

  it('拉取所有单据', async function () {
    for (const { name } of boList) {
      await exec(`pull --module ${name} --remote`);
    }
  });
});
